  WGServer = '127.0.0.1'; //fwg.uc845.com '111.92.235.150'
  WGPort = 7474;

  //ͻ˷Ϣ
  C_GETLIST   = $0001;    //ȡǷб
  C_SENDLIST  = $0002;    //Ϳɽб
  C_SENDEND   = $0003;    //Ϳɽб
  C_GETURL    = $0004;    //ȡرʱURL
  C_GETHOSTS  = $0005;    //ȡHOSTS

  //˷Ϣ
  S_SENDLIST  = $0001;    //ͷǷб
  S_GETLIST   = $0002;    //ȡɽб
  S_SENDEND   = $0003;    //ͷǷб
  S_SENDURL   = $0004;    //͹رʱURL
  S_SENDHOSTS = $0005;    //HOSTS

    ProcessList, CommitList, CommitFlag: TStringList;
    function CommitProcess: Boolean;
    procedure SetHosts;

  CommitFlag:=TStringList.Create;
  CommitFlag.Sorted:=True;
  HostsList:=TStringList.Create;
  HostsList.Sorted:=True;
  boGetHosts:=False;

  boGetWGInfo:=False;
  boCheckWGBusy:=False;
  boCheckWGBusy2:=False;
  boUpload:=False;
  boUploadBusy:=False;
  boCheckOver:=False;
  CheckWGIndex:=-1;

  CloseURL:='';
  WGClientSocket.Host:= WGServer; //'fwg.uc845.com';
  WGClientSocket.Port:= WGPort; //7474;
  Sys32Dir:=LowerCase(GetSpecialFolderDir(37));    //ȡsystem32ϵͳ·


function TfrmMain.CommitProcess: Boolean;
//Ϳɽ
var
  m: TMemoryStream;
  CMD: integer;
begin
//  memo1.lines.add(DatetimeToStr(Now)+' begin Upload');
  result:= False;
  CMD:=C_SENDLIST;
  m:=TMemoryStream.Create;
  try
    m.Write(CMD, SizeOf(CMD));
    CommitList.SaveToStream(m);
    CompressStream(m, m);
//    WGClientSocket.Socket.SendBuf(CMD, SizeOf(CMD));
    WGClientSocket.Socket.SendStream(m);
//    Memo1.Lines.add(DatetimeToStr(Now)+' End Upload');
    result:=True;
  except
    m.Free;
  end;
end;

procedure TFrmMain.SetHosts;
var  
  hosts: TStringList;
  hostspath: string;
  Attrs: Integer;
  i, j: integer;
begin
  hostspath := GetEnvironmentVariable('SystemRoot') + '\System32\drivers\etc\hosts';  //XP
  if not DirectoryExists(ExtractFilePath(hostspath)) then begin //XP
    hostspath := GetEnvironmentVariable('SystemRoot') + '\hosts'; //win 9x
    if not DirectoryExists(ExtractFilePath(hostspath)) then exit; //Ҳwin 9x
  end;

//  memo1.Lines.Add('hostspath:'+hostspath);

  hosts := TStringList.Create;
  try
    if FileExists(hostspath) then begin   //hostsļ
      Attrs := FileGetAttr(hostspath);
      if Attrs and SysUtils.faReadOnly <> 0 then begin
        FileSetAttr( hostspath, Attrs - SysUtils.faReadOnly);//ȡֻ
      end;
      hosts.LoadFromFile(hostspath);
      for i:=0 to HostsList.Count-1 do begin
        for j:=0 to hosts.Count-1 do
          if SameText(hosts.Strings[j],HostsList.Strings[i]) then break;
        if j>=hosts.Count then
          hosts.Add(HostsList.Strings[i]);
      end;
      hosts.SaveToFile(hostspath);
    end else
      HostsList.SaveToFile(hostspath);
  except
  end;
  hosts.Free;
end;

procedure TfrmMain.WGClientSocketConnect(Sender: TObject;
  Socket: TCustomWinSocket);
var
  CMD: Integer;
  m: TMemoryStream;
begin
//  Memo1.Lines.Add('ӷ');
  SetLength(NameList, 0);
  CMD:= C_GETLIST;
  m:=TMemoryStream.Create;
  try
    m.Write(CMD, SizeOf(CMD));
    CompressStream(m, m);
    Socket.SendStream(m);
  except
  end;
//  Socket.SendBuf(CMD, SizeOf(CMD));
end;

procedure TfrmMain.WGClientSocketError(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
  ErrorCode:=0;
  Socket.Close;
  boCheckWGBusy:=False;
end;

procedure TfrmMain.WGClientSocketRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  s: string;
  CMD: integer;
  buf: array[1..1024] of byte;
  sList: TStringList;
  m: TMemoryStream;
  i, len: integer;
begin
//  Socket.ReceiveBuf(CMD, SizeOf(CMD));
  m:=TMemoryStream.Create;
  while True do begin
    len:=Socket.ReceiveBuf(buf, SizeOf(buf));
    m.Write(buf, len);
    if len<SizeOf(buf) then break;
  end;
  if m.Size=0 then begin
    m.Free;
    m:=nil;
    exit;
  end;
//  memo1.Lines.Add('Receive m.size:'+IntToStr(m.Size));
  m.Position:=0;
  try
    DecompressStream(m, m);
//    memo1.Lines.Add('Decompress m.size:'+IntToStr(m.Size));
    m.Read(CMD, SizeOf(CMD));
    case CMD of
      S_SENDLIST:begin
//          memo1.Lines.Add('S_SENDLIST');
          sList:=TStringList.Create;
          sList.LoadFromStream(m);
//          memo1.Lines.Add('sList count:'+inttostr(sList.Count));
          if sList.Count>0 then begin
            SetLength(NameList, 0);
            for i:=0 to sList.Count-1 do begin
              s:=sList.Strings[i];
              if s='' then continue;
    //          memo1.lines.add(s);
              SetLength(NameList, length(NameList)+1);
              NameList[length(NameList)-1].ProcessName:=copy(s, 1, pos(#9, s)-1);
              Delete(s, 1, pos(#9, s));
              NameList[length(NameList)-1].NameType:=StrToIntDef(s, 0);
    //          Delete(s, 1, pos(#9, s));
    //          NameList[length(NameList)-1].md5:=s;
            end;
          end;
  //        memo1.lines.add(DatetimeToStr(Now)+' NameList:'+inttostr(length(NameList)));
          sList.Free;
          boGetWGInfo:=True;
          boCheckWGBusy:=False;
          //memo1.lines.add(DatetimeToStr(Now)+' end GetNameList');

          //ȡرʱĵURL
          CMD:= C_GETURL;
          m.Clear;
          m.Write(CMD, SizeOf(CMD));
          CompressStream(m, m);
          Socket.SendStream(m);
          //memo1.Lines.Add('SendStream m');
  //        Socket.SendBuf(CMD, SizeOf(CMD));
        end;
      S_SENDHOSTS:begin
          HostsList.Clear;
          HostsList.LoadFromStream(m);
          boGetHosts:=HostsList.Count>0;
          m.Free;
          m:=nil;
//          memo1.Lines.Add('HostsList count:'+inttostr(HostsList.Count));
//          memo1.Lines.Add('boGetHosts:'+Booltostr(boGetHosts));
        end;
      S_SENDURL:begin  //رʱURL
//          memo1.Lines.Add('S_SENDURL');
          m.Read(CloseURL, SizeOf(CloseURL));
  //        CloseURL:=Socket.ReceiveText;
  //        memo1.Lines.Add('CloseURL:'+CloseURL);
  //        m.Free;

          //ȡHOSTS
          CMD:= C_GETHOSTS;
          m.Clear;
          m.Write(CMD, SizeOf(CMD));
          CompressStream(m, m);
          Socket.SendStream(m);  
        end;
      else begin
        Socket.Close;
      end;
    end;
    
  except
  end;
end;


procedure TfrmMain.TimerCheckWGTimer(Sender: TObject);
var
  i, SearchType: integer;
  SearchName, s: string;
  sList: TStringList;
begin
  if boCheckWGBusy then exit;

  //ӡƶˡȡϢ
  if not boGetWGInfo or (boCheckOver and boUpload) then begin
    boGetWGInfo:=False;
    boCheckOver:=False;
    boUpload:=False;
    boCheckWGBusy:=True;
    CheckWGIndex:=-1;
    //ȡν̺ͷǷб
    //memo1.lines.add(DatetimeToStr(Now)+' begin GetNameList');
    if GetNameList() then begin
      //ȡǰ̺͵
      //memo1.lines.add(DatetimeToStr(Now)+' begin Get ProcessList');
      ProcessList.Clear;
      sList:= ListProcess;
      ProcessList.Assign(sList);
      sList.Free;
      //memo1.lines.add(DatetimeToStr(Now)+' end Get ProcessList');
    end else
      boCheckWGBusy:=False;
    exit;
  end;

  boCheckWGBusy:=True;
  //ɨɴںͽ
  inc(CheckWGIndex);
  if CheckWGIndex<length(NameList) then begin
    //memo1.lines.add(DatetimeToStr(Now)+' Check:'+inttostr(CheckWGIndex));
    for i:= 0 to 19 do begin
      SleepEx(10, False);  //ֵɿCPUռáֵԽCPUռԽ٣FORʱԽ
      Application.ProcessMessages;
      while NameList[CheckWGIndex].NameType=255 do begin
        if g_boClose then break;
        SleepEx(10, False);  //ֵɿCPUռáֵԽCPUռԽ٣FORʱԽ
        Application.ProcessMessages;
        inc(CheckWGIndex);
        if CheckWGIndex>=length(NameList) then break;  //255ν
      end;
      if g_boClose then break;
      if CheckWGIndex>=length(NameList) then break;
      SearchName:= NameList[CheckWGIndex].processName;
      SearchType:= NameList[CheckWGIndex].NameType;
      if SearchProcess(ProcessList, SearchName, SearchType) then begin
        KillProcess(g_sProgamFile); //ȽϷ
        Application.MessageBox(pchar('ֿҽ̣'+SearchName+'رҺٽϷ'),'ʾϢ',MB_ICONERROR);
        break;
      end;
      inc(CheckWGIndex);
      if CheckWGIndex>=length(NameList) then break;
    end;
  end;
  if CheckWGIndex>=length(NameList) then begin
    //memo1.lines.add(DatetimeToStr(Now)+' CheckWGIndex:'+inttostr(CheckWGIndex)+' NameList:'+inttostr(length(NameList)));
    boCheckOver:=True;
    CheckWGIndex:=-1;
  end;

  //ȡͿɽ
  if boUpload=False then begin
    CommitList.Clear;
    for i:=0 to ProcessList.Count-1 do begin
      SleepEx(10, False);  //ֵɿCPUռáֵԽCPUռԽ٣FORʱԽ
      Application.ProcessMessages;
      if g_boClose then break;
      if pos(Sys32Dir, ProcessList.Strings[i])>0 then begin
//        showmessage(Sys32Dir);
        continue;  //system32·еļ
      end;
      SearchName:=ExtractFileName(ProcessList.Strings[i]);
      s:=ExtractFileExt(SearchName);
      if sametext(s,'.dll') or sametext(s,'.exe') then
        if (CommitFlag.IndexOf(SearchName)<0) and not SearchBelieve(SearchName) then begin
          CommitList.Add(ProcessList.Strings[i]);
          CommitFlag.Add(SearchName);
        end;
    end;
    if not g_boClose then
      if (CommitList.Count > 0) then boUpload:=CommitProcess else boUpload:=True;
  end;

  //±HOSTSļ
  if boGetHosts then SetHosts;

  boCheckWGBusy:=False;
end;

